package views;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import util.Constants;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Activity Class for displaying a list of hits
 * @author Simon
 *
 */
public class SearchHits extends Activity {
	private TableLayout tl;
	private ScrollView sv;
	private TableRow tmpRow;
	private TextView tmpText;
	private Button tmpButton;
	private HashMap<String, Integer> returnMap;
	private Map<String, Integer> queryMap;
	private boolean isAltered;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		isAltered = false;
		returnMap = new HashMap<String, Integer>();
		queryMap = (HashMap<String, Integer>)this.getIntent().getSerializableExtra("queryMap");
		sv = new ScrollView(this);
		tl = new TableLayout(this);
		
		/* Adds the necessary rows to the view */
		for(Entry<String, Integer> entry : queryMap.entrySet()){
			tl.addView(createListElement(entry));
		}
		sv.addView(tl);
		setContentView(sv);
		
		
		
		
	}
	
	/**
	 * @param queryEntry to be made into a row
	 * @return TableRow representing one search hit
	 * 
	 */
	private TableRow createListElement(final Entry<String, Integer> queryEntry) {
		tmpRow = new TableRow(this);
		tmpText = new TextView(this);
		tmpText.setText(queryEntry.getKey());
		tmpRow.addView(tmpText);
		tmpButton = new Button(this);
		tmpButton.setText("Add");
		
		
		tmpButton.setOnClickListener(new View.OnClickListener(){
			public void onClick(View v){
				returnMap.put(queryEntry.getKey(), queryEntry.getValue());
				isAltered = true;
				Toast.makeText(SearchHits.this, queryEntry.getKey() + " added.", Toast.LENGTH_SHORT).show();
			}
		});
		tmpRow.addView(tmpButton);
		tmpButton = new Button(this);
		tmpButton.setText("View");
		
		tmpButton.setOnClickListener(new View.OnClickListener(){
			public void onClick(View v){
				Intent outIntent = new Intent();
				if(isAltered){
					outIntent.putExtra("selectedMap", returnMap);
					outIntent.putExtra("searchCode", queryEntry.getValue());

					setResult(Constants.RESPONSESHSTARTVIEWALTERED, outIntent);
					finish();
				} else {
					outIntent.putExtra("searchCode", queryEntry.getValue());

					setResult(Constants.RESPONSESHSTARTVIEW, outIntent);
				
					finish();
				}
				//Toast.makeText(SearchHits.this, "This does naught yet.", Toast.LENGTH_SHORT).show();
			}
		});
		tmpRow.addView(tmpButton);
		return tmpRow;
		
		
	}
	
	/*Captures the back key*/
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
	    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
	    	Intent outIntent = new Intent();
			if(isAltered){
				outIntent.putExtra("selectedMap", returnMap);

				setResult(Constants.RESPONSESHSTARTSEARCHALTERED, outIntent);
				finish();
			} else {

				setResult(Constants.RESPONSESHSTARTSEARCH, outIntent);
			
				finish();
			}
	        return true;
	    }
	    return super.onKeyDown(keyCode, event);
	}
	

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		returnMap = new HashMap<String, Integer>();
		// TODO Auto-generated method stub
		super.onResume();
	}

	
	
	
	
	
	
}
